//
//  NetworkConfig.swift
//  UAKA
//
//  Created by 王剑鹏 on 2020/4/26.
//  Copyright © 2020 waing. All rights reserved.
//

import UIKit
import ObjectMapper
import Moya

///// 参数构造器
//struct Params {
//    typealias T = Params
//
//    /// 最终参数
//    var parameters = Dictionary<String,Any>()
//
//    /// 设置参数
//    /// - Parameters:
//    ///   - key: key
//    ///   - value: value
//    func addParameters(key: String, value: Any) -> T {
//        var newValue = self
//        newValue.parameters[key] = value
//        return newValue
//    }
//
//    /// 设置链接
//    /// - Parameter route: 链接
//    func setRoute(_ route: String) -> T{
//        return addParameters(key: "route", value: route)
//    }
//
//    /// 基本参数
//    static var base = T.init(parameters: ["version":"1"])
//
//}
/**
 //使用
  let param = Params.base.setRoute("app/getData").addParameters(key: "name", value: name).addParameters(key: "age", value: age).parameters
 */


/// 参数构造器
typealias ParamDictionary = Dictionary<String, Any>
/// 基本参数
var BaseParameters : ParamDictionary = ["version":"1"]

extension Dictionary where Key == String,Value == Any {
    /// 设置参数
    /// - Parameters:
    ///   - key: key
    ///   - value: value
     func addParams(key: String, value: Any) -> ParamDictionary {
        var newVlue = self
        newVlue[key] = value
        return newVlue
    }
    
    /// 设置链接
    /// - Parameter route: 链接
     func setRoute(_ route: String) -> ParamDictionary{
        return self.addParams(key: "route", value: route)
    }
}

let baseUrl : URL = URL.init(string: "https://www.uaka.com/api_plus/")!


/// 网络请求根数据
class BaseModel: Mappable {
    required init?(map: Map) {
        
    }
    func mapping(map: Map) {
        status      <- map["status"]
        text        <- map["text"]
        data        <- map["data"]
    }
    var status: Int = 1
    var text: String = ""
    var data: [String: Any]?
}


/// 服务器网络错误
enum LENetworkError : Swift.Error {
    
    /// 状态码错误
    case statusError(BaseModel)
    /// model解析错误
    case modelError(String)
}


extension LENetworkError {
    var message : String {
        switch self {
        case .statusError(let model): return model.text
        case .modelError(let className): return ("数据解析错误".localized + className)
        }
    
    }
}
extension String {
    var localized: String {
        return NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: "", comment: "")
    }
    
    func localized(withComment:String) -> String {
        return NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: "", comment: withComment)
    }
}
